package com.guagua.chat.post;
/**
 * 北京八度互联科技有限公司版权所有
 * Copyright (C) Badu Corporation. All Rights Reserved
 */
import java.util.Map;

import net.sf.json.JSONObject;
import net.sf.json.JSONUtils;

import com.guagua.chat.base.BaseAction;
import com.opensymphony.webwork.ServletActionContext;
/**
 * 房间首页-贴子
 * @author liyi
 * @version 2012-7-2 9:00:00
 */
public class PostAction extends BaseAction{
	
	private String roomId;//房间ID
	
	private int roomMaster=0;//是否是房主1是 0否
	
	private int pageNo ;
	
	private PostManager _tManager = PostManager.getInstance();
	private long userId;
	
	public String getRoomId() {
		return roomId;
	}
	public void setRoomId(String roomId) {
		this.roomId = roomId;
	}
	public void setUserId(long userId) {
		this.userId = userId;
	}
	
	
	
	public int getRoomMaster() {
		return roomMaster;
	}
	public void setRoomMaster(int roomMaster) {
		this.roomMaster = roomMaster;
	}
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	
	/**
	 * 新发布贴子主题
	 * @return 如果成功 跳转
	 * @throws Exception 
	 */
	public String createPost() throws Exception{
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		String content = request.getParameter("content");
		String title = request.getParameter("title");
		//封装对象
		PostInfo _portInfo = new PostInfo();
		_portInfo.setContent(content);
		_portInfo.setOp_time(System.currentTimeMillis());
		_portInfo.setCreate_time(System.currentTimeMillis());
		_portInfo.setRoom_id(Long.parseLong(roomId));
		_portInfo.setTitle(title);
		_portInfo.setUser_id(getAuthToken().getUserID());
		_portInfo.setUser_nick(this.getAuthToken().getNickname());
		boolean val = _tManager.createPostOrComment(_portInfo, roomId);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", val);
		response.setHeader("Content-type", "application/json");
		response.setHeader("Cache-Control","no-cache");
		response.setHeader("expires", "-1");
		response.getWriter().write(jsonObject.toString());
		return null;
	}
	/**
	 * 查找所有POST主体并分页显示
	 * @return
	 * @throws Exception
	 */
	public String findPostAll()throws Exception{
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		
		String pageNoStr = request.getParameter("pageNo");
		int pageNo = 1;
		if(pageNoStr!=null && !pageNoStr.equals("")){
			pageNo = Integer.parseInt(pageNoStr);
		}
		Map<String,Object> map = _tManager.findPostAllByRoomId(roomId, pageNo);
		map.put("pageNo", pageNo);
	    request.setAttribute("resultData", map);
	    this.userId = this.getUserId();
	    
		return "POSTLIST";
	}
	/**
	 * 删除贴子主题
	 * @return
	 * @throws Exception
	 */
	public String delPostById()throws Exception{
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		long id = Long.parseLong(request.getParameter("id"));//topicId
		boolean val = _tManager.delPostById(roomId, id);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", val);
		response.setHeader("Content-type", "application/json");
		response.setHeader("Cache-Control","no-cache");
		response.setHeader("expires", "-1");
		response.getWriter().write(jsonObject.toString());
		return null;
	}
	/**
	 * 加载POST
	 * @return
	 * @throws Exception
	 */
	public String findPostInfo()throws Exception{
		this.userId = getUserId();
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		
		String pageNoStr = request.getParameter("pageNo");
		long postId = Long.parseLong(request.getParameter("id"));//topicId
		int pageNo = 1;
		if(pageNoStr!=null && !pageNoStr.equals("")){
			pageNo = Integer.parseInt(pageNoStr);
		}
		Map<String,Object> map = _tManager.findPostCommentsByRoomId(roomId, postId,pageNo);
		map.put("pageNo", pageNo);
		map.put("postId", postId);
		request.setAttribute("resultData", map);
		return "POSTINFO";
	}
	
	/**
	 * 新发布贴子主题
	 * @return 如果成功 跳转
	 * @throws Exception 
	 */
	public String createComment() throws Exception{
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		long parent_id = Long.parseLong(request.getParameter("parentId"));
		String content = request.getParameter("content");
		//封装对象
		PostInfo _portInfo = new PostInfo();
		_portInfo.setContent(content);
		_portInfo.setOp_time(System.currentTimeMillis());
		_portInfo.setCreate_time(System.currentTimeMillis());
		_portInfo.setRoom_id(Long.parseLong(roomId));
		_portInfo.setUser_id(getAuthToken().getUserID());
		_portInfo.setUser_nick(getAuthToken().getNickname());
		_portInfo.setParent_id(parent_id);
		boolean val = _tManager.createPostOrComment(_portInfo, roomId);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", val);
		response.setHeader("Content-type", "application/json");
		response.setHeader("Cache-Control","no-cache");
		response.setHeader("expires", "-1");
		response.getWriter().write(jsonObject.toString());
		return null;
	}
	/**
	 * 删除评论
	 * @return
	 * @throws Exception
	 */
	public String delCommentById()throws Exception{
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		long id = Long.parseLong(request.getParameter("id"));//topicId
		long parentId = Long.parseLong(request.getParameter("parentId"));//parentId
		boolean val = _tManager.delCommentById(id,parentId,roomId);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", val);
		response.setHeader("Content-type", "application/json");
		response.setHeader("Cache-Control","no-cache");
		response.setHeader("expires", "-1");
		response.getWriter().write(jsonObject.toString());
		return null;
	}

}
